package klimuts.periodicals.admin.actions;

import java.util.LinkedList;
import java.util.Map;

import klimuts.periodicals.admin.Order;
import klimuts.periodicals.data.DataSource;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 *   Action prepare orders data for orders page and show this page after all.
 * For reduce data base requests all orders will be once saved in session and   
 * next time will be getting from it. 
 * @author Klimuts Siarhei
 */
public class ShowOrders extends ActionSupport implements SessionAware{
	private LinkedList<Order> orders;
	private Map<String,Object> session;
	
	public String execute() throws Exception{
		orders=(LinkedList<Order>)session.get("orders");
		if ((orders==null)||(orders.getLast().getId()<DataSource.getMax("Order"))) {
			orders=DataSource.getOrders();
			session.put("orders", orders);
		}
		return "success";
	}

	public LinkedList<Order> getOrders() {
		return orders;
	}

	public void setOrders(LinkedList<Order> orders) {
		this.orders = orders;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}


}
